package com.mhh.selary.backend.controller;

import com.mhh.sdk.beans.dto.BaseItemDTO;
import com.mhh.sdk.beans.dto.DropDownListDTO;
import com.mhh.selary.backend.api.config.ISelaryConfigService;
import com.mhh.selary.backend.api.project.ISelaryProjectService;
import com.mhh.selary.backend.enums.ProjectScopeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mhh
 * @date 2020-09-15 11:06:12
 */
@Slf4j
@RestController
@Api(tags = "公共接口")
@RequestMapping("/common")
public class CommonController {

    @Autowired
    private ISelaryProjectService selaryProjectService;

    @Autowired
    private ISelaryConfigService selaryConfigService;

    @PostMapping("projectDropDownList")
    @ApiOperation("项目选择下拉框")
    public DropDownListDTO projectDropDownList() {
        List<BaseItemDTO> itemList = selaryProjectService.findAll().stream()
                .filter(projectDTO -> ProjectScopeEnum.PUBLIC.getScope().equals(projectDTO.getProjectScope()))
                .map(projectDTO -> BaseItemDTO.builder()
                        .itemId(projectDTO.getId())
                        .itemName(projectDTO.getProjectName())
                        .itemCode(projectDTO.getProjectCode())
                        .build())
                .collect(Collectors.toList());
        return DropDownListDTO.builder().dataList(itemList).build();
    }

    @PostMapping("envDropDownList")
    @ApiOperation("环境选择下拉框")
    public DropDownListDTO envDropDownList() {
        List<BaseItemDTO> itemList = selaryConfigService.findAll().stream()
                .map(envDTO -> BaseItemDTO.builder()
                        .itemId(envDTO.getEnvId())
                        .itemName(envDTO.getEnvName())
                        .build())
                .collect(Collectors.toList());
        return DropDownListDTO.builder().dataList(itemList).build();
    }
}
